package com.huizluo.module3.hashmap;

import java.util.HashMap;
import java.util.Map;

public class CountStr {
    private HashMap<String,Integer> countMap;
    private void calculate(String str){
        String[] strArr = str.split(",");
        for (String item: strArr){
            Integer v;
            if (this.countMap.containsKey(item)){
                 v = this.countMap.get(item);
                v++;
            }else{
                v = 1;
            }
            this.countMap.put(item,v);
        }
    }

    public CountStr(HashMap<String, Integer> countMap) {
        this.countMap = countMap;
    }

    public void print(){
       for (Map.Entry<String,Integer>entry:this.countMap.entrySet()){
           System.out.println("[" + entry.getKey() + "] 出现了 ["+entry.getValue()+"]次" );
       }
    }

    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        CountStr counter = new CountStr(map);
        String str = "123,456,789,123,456";
        counter.calculate(str);
        counter.print();
    }
}
